package com.haredot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haredot.entity.Message;
import com.haredot.service.MessageService;
import com.haredot.vo.R;
import com.haredot.vo.UserBlackVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageService messageService ;

    /**
     *  1. 第一次查询的时候，只查询 好友的发来的未读消息 (无论多少条)
     *     1.1  如果没有查询到、则 查询最近5条消息返回
     *     1.2  如果查询到了未读消息，将未读消息设置为已读。并返回！
     *
     * @param offset : 偏移量，默认0，下次的偏移量为 页面已有数据的个数
     * @param unread :  unread 代表未读数量
     */
    @GetMapping("/records")
    public R<Message>  list(@RequestParam(value = "offset", required = false, defaultValue = "0") Long offset,
                            @RequestParam(value = "unread", required = false, defaultValue = "0") Long unread,
                            @RequestParam(value="uid") Long toUserId) {

        // 查询聊天记录
        return messageService.queryMessageRecords(offset, unread,  toUserId) ;

    }

    @PutMapping("/update/{messageId}")
    public R<?> updateMessageState(@PathVariable("messageId") Long messageId) {
        return messageService.updateMessage(messageId) ;
    }

    /**
     * 查询黑名单用户列表
     */
    @GetMapping("/black/list")
    public R<?> blackList(@RequestParam(value = "page", required = false, defaultValue = "1") Long current,
                          @RequestParam(value = "rows", required = false, defaultValue = "10") Long rows) {
        Page<UserBlackVo> page = new Page<>(current, rows) ;
        return messageService.queryBlackList(page);
    }

    /**
     * 添加黑名单用户
     * @param blackUser
     * @return
     */
    @PostMapping("/black/{id:\\d+}")
    public R<?> bindBlack(@PathVariable("id") Long blackUser) {

        return messageService.bindBlackUser(blackUser);
    }

    /**
     * 解除黑名单
     * @param id
     * @return
     */
    @DeleteMapping("/black/{id:\\d+}")
    public R<?> unbindBlack(@PathVariable("id") Long id) {

        return messageService.removeBlackUser(id);
    }
}
